package com.yuanli.cosmeticmirror.utils;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.Settings;
import android.widget.Toast;

import java.io.File;

public class RingUtils {
    /**
     * 设置铃声
     *
     * @param type  RingtoneManager.TYPE_RINGTONE 来电铃声
     *              RingtoneManager.TYPE_NOTIFICATION 通知铃声
     *              RingtoneManager.TYPE_ALARM 闹钟铃声
     * @param path  下载下来的mp3全路径
     * @param title 铃声的名字
     */
    public static void setRing(Context context, int type, String path, String title) {
        try {
            // 系统当前 通知铃声
            Uri oldRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);
            // 系统当前 通知铃声
            Uri oldNotification = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);
            // 系统当前 闹钟铃声
            Uri oldAlarm = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM);

            File sdfile = new File(path);
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, title);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, true);

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
            Uri newUri = null;
            String deleteId = "";
            Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null);
            if (cursor.moveToFirst()) {
                deleteId = cursor.getString(cursor.getColumnIndex("_id"));
            }

            context.getContentResolver().delete(uri,
                    MediaStore.MediaColumns.DATA + "=\"" + sdfile.getAbsolutePath() + "\"", null);
            newUri = context.getContentResolver().insert(uri, values);


            if (newUri != null) {

                String ringStoneId = "";
                String notificationId = "";
                String alarmId = "";
                if (null != oldRingtoneUri) {
                    ringStoneId = oldRingtoneUri.getLastPathSegment();
                }

                if (null != oldNotification) {
                    notificationId = oldNotification.getLastPathSegment();
                }

                if (null != oldAlarm) {
                    alarmId = oldAlarm.getLastPathSegment();
                }

                Uri setRingStoneUri;
                Uri setNotificationUri;
                Uri setAlarmUri;

                if (type == RingtoneManager.TYPE_RINGTONE || ringStoneId.equals(deleteId)) {
                    setRingStoneUri = newUri;

                } else {
                    setRingStoneUri = oldRingtoneUri;
                }

                if (type == RingtoneManager.TYPE_NOTIFICATION || notificationId.equals(deleteId)) {
                    setNotificationUri = newUri;

                } else {
                    setNotificationUri = oldNotification;
                }

                if (type == RingtoneManager.TYPE_ALARM || alarmId.equals(deleteId)) {
                    setAlarmUri = newUri;

                } else {
                    setAlarmUri = oldAlarm;
                }


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Settings.System.canWrite(context)) {
                        Toast.makeText(context, "设置铃声需允许修改系统设置", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, setRingStoneUri);
                    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION, setNotificationUri);
                    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, setAlarmUri);
                } else {
                    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, setRingStoneUri);
                    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION, setNotificationUri);
                    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, setAlarmUri);
                }
                switch (type) {
                    case RingtoneManager.TYPE_RINGTONE:
                        Toast.makeText(context.getApplicationContext(), "设置来电铃声成功！", Toast.LENGTH_SHORT).show();
                        break;
                    case RingtoneManager.TYPE_NOTIFICATION:
                        Toast.makeText(context.getApplicationContext(), "设置短信铃声成功！", Toast.LENGTH_SHORT).show();
                        break;
                    case RingtoneManager.TYPE_ALARM:
                        Toast.makeText(context.getApplicationContext(), "设置闹钟铃声成功！", Toast.LENGTH_SHORT).show();
                        break;
                        default:break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
